<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/19
 * Time: 14:27
 */
namespace App\Http\Controllers\admin_system;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Module\EntManageuser;
use App\Module\EntMindShare;
use Storage;
use Cookie;
use DB;
use Validator;
use Redirect;
use Session;

class MindShareController extends Controller
{
    public function index($keyword = '')
    {
        if ($keyword != '') {
            $mindShare = DB::table('VW_MindShare')->select('MinSgaID', 'MinSgaTitle', 'IsShow', 'MasterID', 'ReleaseDate', 'RealName')
                ->where('DeleteMark', '1')
                ->where('MinSgaTitle', 'like', '%' . $keyword . '%')
                ->orderBy('ReleaseDate', 'desc')
                ->paginate(20);
        } else {
            $mindShare = DB::table('VW_MindShare')->select('MinSgaID', 'MinSgaTitle', 'IsShow', 'MasterID', 'ReleaseDate', 'RealName')
                ->where('DeleteMark', '1')
                ->orderBy('ReleaseDate', 'desc')
                ->paginate(20);
        }
        return view('admin_system.mindshare.index', [
            'mindShare' => $mindShare,
            'keyword' => $keyword
        ]);
    }

    public function createindex()
    {
        return view('admin_system.mindshare.indexAdd');
    }

    public function storeindex(Request $request)
    {
        $AName = Session::get('AName');
        $AKey = Session::get('AKey');
        if ($AKey != '' && $AName != '') {
            $entManageuser = EntManageuser::select('UserID')->where('UserName', $AName)
                ->where('UserPwd', md5($AKey))
                ->where('DeleteMark', 1)
                ->first();
        }
        if ($entManageuser) {
            $entMindShare = new EntMindShare();
            $entMindShare->MinSgaTitle = trim($request->get('txtMinSgaTitle'));
            $entMindShare->MinSgaDesc = trim($request->get('txtMinSgaDesc'));
            $entMindShare->ShareContents = htmlentities($request->get('txtAreaCkeditor'));
            $FilePath1 = '';
            if ($request->get('File_1') != '') {
                $FilePath1 = $request->get('File_1');
            }
            $entMindShare->FilePath1 = $FilePath1;
            $entMindShare->MasterID = $entManageuser->UserID;
            $entMindShare->DeleteMark = 1;
            $entMindShare->IsShow = $request->get('RadIsShow');
            $entMindShare->ReleaseDate = date('Y-m-d H:i:s', time());
            $entMindShare->AddDate = date('Y-m-d H:i:s', time());
            $entMindShare->save();
            $result = '';
            if ($entMindShare) {
                $result = '信息添加成功！';
            } else {
                $result = '信息添加失败！';
            }
            return redirect::back()->withInput()->withErrors($result);
        }
    }

    public function indexedit($id)
    {
        try {
            $entMindShare = EntMindShare::findOrFail($id);
        } catch (ModelNotFoundException $e) {
            throw new NotFoundHttpException();
        }
        return view('admin_system.mindshare.indexEdit', ['entMindShare' => $entMindShare]);
    }

    public function indexupdate(Request $request, $id)
    {
        $AName = Session::get('AName');
        $AKey = Session::get('AKey');
        if ($AKey != '' && $AName != '') {
            $entManageuser = EntManageuser::select('UserID')->where('UserName', $AName)
                ->where('UserPwd', md5($AKey))
                ->where('DeleteMark', 1)
                ->first();
        }
        if ($entManageuser) {
            $entMindShare = EntMindShare::find($id);
            $entMindShare->MinSgaTitle = trim($request->get('txtMinSgaTitle'));
            $entMindShare->MinSgaDesc = trim($request->get('txtMinSgaDesc'));
            $entMindShare->ShareContents = htmlentities($request->get('txtAreaCkeditor'));
            $FilePath1 = '';
            if ($request->get('File_1') != '') {
                $FilePath1 = $request->get('File_1');
            }
            $entMindShare->FilePath1 = $FilePath1;
            $entMindShare->MasterID = $entManageuser->UserID;
            $entMindShare->IsShow = $request->get('RadIsShow');
            $entMindShare->ReleaseDate = date('Y-m-d H:i:s', time());
            $entMindShare->save();
            $result = '';
            if ($entMindShare) {
                $result = '信息修改成功！';
            } else {
                $result = '信息修改失败！';
            }
        }
        return redirect::back()->withInput()->withErrors($result);
    }

    public function indexdelete(Request $request)
    {
        $result = '';
        try {
            if ($request->ajax()) {
                $IDList = $request->json('IDList');
                if (is_numeric($IDList)) {
                    EntMindShare::where('MinSgaID', $IDList)->update([
                        'DeleteMark' => '0'
                    ]);
                    $result = 'ok';
                } else {
                    EntMindShare::whereIn('MinSgaID', explode(',', $IDList))->update([
                        'DeleteMark' => '0'
                    ]);
                    $result = 'ok';
                }
            }
        } catch (Exception $e) {
            $result = 'no';
        }
        return $result;
    }
    public function preview(Request $request, $id)
    {
        try {
            $entMindShare = EntMindShare::findOrFail($id);
        } catch (ModelNotFoundException $e) {
            throw new NotFoundHttpException();
        }
        return view('admin_system.mindshare.preview', ['entMindShare' => $entMindShare]);
    }

    // public function ajx(Request $request,$id){
    //     $data = EntMindShare::FindOrFail($id);
    //     return view('admin_system.mindshare.index',['data1'=>$data]);
    // } 
    public function ajx(Request $request)
    {
        $adId = $request->input('id');

     // $entMindShare = EntMindShare::findOrFail($adId)
        $data1 = DB::table('mindshare')
        ->where('MinSgaId',$adId)
        ->get();
        // return $entMindShare;
        return $data1;
    }
}